package com.t.bricks.bussiness.service.bridge.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.stereotype.Service;

import com.t.bricks.bussiness.dao.bridge.BridgeDao;
import com.t.bricks.bussiness.service.bridge.UserAndRightService;
import com.t.bricks.model.MsgEmity;

/**
 * 用户权限接口桥接实现
 * @author huangxinbian
 */
@ConditionalOnMissingClass("com.t.bricks.bussiness.service.userandright.impl.UserAndRightServiceImpl")
@Service
public class UserAndRightServiceImpl implements UserAndRightService {

	@Autowired
	private BridgeDao bridgeDao;

	/**
	 * 桥接到登录服务的服务名,默认TBaseSystemServer
	 */
	@Value("${CloudServerBaseServer:TBaseSystemServer}")
	private String userAndRightServerName;

	/**
	 * 清理指定用户的缓存
	 * @param sUser 用户编号
	 * @return
	 */
	@Override
	public MsgEmity clearCache(String sLoginId) {
		Map<String, Object> map = new HashMap<>(1);
		map.put("sLoginId", sLoginId);
		
		MsgEmity me = bridgeDao.post(userAndRightServerName, "/user/and/right/clear/cache",
				map, MsgEmity.class, false);
		if (!me.isSuccess()) {
			return me;
		}
		
		return me.getData();
	}
	
	/**
	 * 根据用户取权限标识字符串(一个权限标识代表了多个可访问的url路径)
	 * 不用判断请求用户是谁,因为其它人获取信息后没用,权限校验会在每次进行具体操作时进行再次判断
	 * @param sUserId
	 * @return
	 */
	@Override
	public MsgEmity findEnglishByUserId(String sUserId) {
		Map<String, Object> map = new HashMap<>(1);
		map.put("sUserId", sUserId);
		
		MsgEmity me = bridgeDao.post(userAndRightServerName, "/user/and/right/find/english",
				map, MsgEmity.class, false);
		if (!me.isSuccess()) {
			return me;
		}
		
		return me.getData();
	}

	/**
	 * 验证指定用户是否有访问指定url的权限(给内部拦截器用,直接返回Boolean)
	 * @param sUserId
	 * @param url
	 * @return
	 */
	@Override
	public boolean checkUrlRight(String sUserId, String url) {
		Map<String, Object> map = new HashMap<>(2);
		map.put("sUserId", sUserId);
		map.put("url", url);
		
		MsgEmity me = bridgeDao.post(userAndRightServerName, "/user/and/right/check",
				map, MsgEmity.class, false);
		if (!me.isSuccess()) {
			return false;
		}
		
		return ((MsgEmity)me.getData()).isSuccess();
	}
	
	/**
	 * 验证指定用户是否有访问指定url的权限
	 * @param sUserId 验证的用户
	 * @param url 请求验证的权限(URL地址)
	 * @return
	 */
	@Override
	public MsgEmity checkRight(String sUserId, String url) {
		Map<String, Object> map = new HashMap<>(2);
		map.put("sUserId", sUserId);
		map.put("url", url);
		
		MsgEmity me = bridgeDao.post(userAndRightServerName, "/user/and/right/check/right",
				map, MsgEmity.class, false);
		if (!me.isSuccess()) {
			return me;
		}
		
		return me.getData();
	}
	
	/**
	 * 根据用户查询用户所拥有的权限编号集合
	 * @param sUserId
	 * @return
	 */
	@Override
	public MsgEmity findRightId(String sUserId) {
		Map<String, Object> map = new HashMap<>(1);
		map.put("sUserId", sUserId);
		
		MsgEmity me = bridgeDao.post(userAndRightServerName, "/user/and/right/find/rightid",
				map, MsgEmity.class, false);
		if (!me.isSuccess()) {
			return me;
		}
		
		return me.getData();
	}
	
	/**
	 * 根据用户查询用户所拥有的权限的最后更新时间
	 * @param sUserId
	 * @return
	 */
	@Override
	public MsgEmity lastTime(String sUserId) {
		Map<String, Object> map = new HashMap<>(1);
		map.put("sUserId", sUserId);
		
		MsgEmity me = bridgeDao.post(userAndRightServerName, "/user/and/right/lasttime",
				map, MsgEmity.class, false);
		if (!me.isSuccess()) {
			return me;
		}
		
		return me.getData();
	}
	
}
